package com.atguigu.ggkt.exception;

import com.atguigu.ggkt.result.Result;
import io.jsonwebtoken.ExpiredJwtException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice  //aop
public class GlobalExceptionHandler {

    //全局异常处理
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e) {
        System.out.println("全局.....");
        e.printStackTrace();
        return Result.fail(null).message("执行全局异常处理");
    }

    /**
     * 登陆过期异常处理
     * io.jsonwebtoken.ExpiredJwtException: JWT expired at 2024-08-15T18:48:03Z. Current time: 2024-08-19T18:48:41Z, a difference of 345638179 milliseconds.  Allowed clock skew: 0 milliseconds.
     * at io.jsonwebtoken.impl.DefaultJwtParser.parse(DefaultJwtParser.java:385)
     * @author Tony Xi
     * @since 20240819
     */
    @ExceptionHandler(ExpiredJwtException.class)
    @ResponseBody
    public Result error(ExpiredJwtException e) {
        System.out.println("登录过期，请重新登录.....");
        e.printStackTrace();
        return Result.fail(null).message("登录过期，请重新登录").code(20005);
    }

    //特定异常处理ArithmeticException
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result error(ArithmeticException e) {
        System.out.println("特定.....");
        e.printStackTrace();
        return Result.fail(null).message("执行ArithmeticException异常处理");
    }

    //自定义异常处理GgktException
//    @ExceptionHandler(GgktException.class)
//    @ResponseBody
//    public Result error(GgktException e) {
//        e.printStackTrace();
//        return Result.fail(null).code(e.getCode()).message(e.getMsg());
//    }

}
